/**
 * Copyright (c) 2009 Blue Whale Systems Ltd
 * 
 * This software is provided "as is," and the copyright holder makes no representations or warranties, express or
 * implied, including but not limited to warranties of merchantability or fitness for any particular purpose or that the
 * use of this software or documentation will not infringe any third party patents, copyrights, trademarks or other
 * rights.
 * 
 * The copyright holder will not be liable for any direct, indirect special or consequential damages arising out of any
 * use of this software or documentation.
 */
package com.bluewhalesystems.client.lwuit.richtext;

import java.io.*;

import com.sun.lwuit.RichTextArea;

/**
 * Generates 'line data' for a {@link RichTextAreaModel} given 'formatted data' created by {@link FormattedDataGenerator}. 
 * This code exists in a separate class for several reasons:
 * 
 * <ul>
 * <li> it can be reused by various implementations of {@link RichTextAreaModel}
 * <li> it can be run in the background for large data sets
 * </ul>
 * 
 * 'Line data' consists of version indicator (a single byte) followed by ...
 */
public final class LineDataGenerator implements Runnable
{
    public void setRichTextAreaModel( RichTextAreaModel aModel )
    {
        iModel = aModel;
    }

    public void setFormattedDataGenerator( FormattedDataGenerator aFormattedDataGenerator )
    {

    }

    public void setWidth( int aWidth )
    {

    }

    public int getHeight()
    {
        return iHeight;
    }

    public GrowableCharArray getFormattedData()
    {
        return null;
    }

    /**
     * Returns the resulting line data.
     * 
     * @return line data or null
     */
    public byte[] getLineData()
    {
        return iLineData;
    }

    // Runnable

    public void run()
    {
        try
        {
        }
        catch( Throwable t )
        {
            t.printStackTrace();
        }
    }

    /**
     * Replaces unprintable characters (such as tabs and carriage returns) with spaces.
     * 
     * Note: This implementation is used for (and is meant for) short strings. It is more efficient to use 
     * {@link String#replace(char, char)} multiple times when you're not sure how short the string is.
     * 
     * @param aChars the input character array
     * @param aOffset beginning offset of the substring to consider
     * @param aDelta length of the substring to consider
     */
    static void replaceUnprintableCharacters( char[] aChars, int aOffset, int aDelta )
    {
        for( int index = aOffset; index < aOffset + aDelta; ++index )
        {
            switch( aChars[index] )
            {
                case ASCII_TAB:
                case ASCII_CR:
                    // Replace these unprintable characters with spaces.
                    aChars[index] = ASCII_SPACE;
                    break;
            }
        }
    }

    private static final int  MIN_WIDTH_REQUIRED                   = 15;
    private static final int  MIN_LENGTH_BEFORE_INSERTING_ELLIPSIS = 10;

    private static final char ASCII_CR                             = 0x0D;
    private static final char ASCII_LF                             = 0x0A;
    private static final char ASCII_TAB                            = 0x09;
    private static final char ASCII_SPACE                          = 0x20;
    private static final char ASCII_ELLIPSIS                       = 0x85;

    private RichTextAreaModel iModel;
    private int               iWidth;
    private int               iHeight;
    private byte[]            iLineData;
}
